package com.guli.product.app;

import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;
import com.guli.product.entity.AttrGroupEntity;
import com.guli.product.service.AttrGroupService;
import com.guli.product.vo.AttrGroupWithAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 属性分组
 * @author zzb
 * @email 1626131069@qq.com
 * @date 2023-10-21 10:18:56
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    /**
     * 获取当前分类下的所有属性分组并且获取其所有属性
     * @param catId
     * @return
     */
    @GetMapping("/{catId}/withattr")
    public R catagoryWithAttr(@PathVariable Long catId) {
        List<AttrGroupWithAttrVo> attrGroupWithAttrVos = attrGroupService.catagoryWithAttr(catId);
        return R.ok().put("data", attrGroupWithAttrVos);
    }


    /**
     * 根据分类ID查询该商品所有属性分组信息
     * @param catId
     * @return
     */
    @GetMapping("/list/{catId}")
    public R list(@PathVariable Long catId, @RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(catId, params);
        return R.ok().put("page", page);
    }

    /**
     * 查询某个商品分组的信息
     * @param attrGroupId
     * @return
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        //查询属性分组信息
        AttrGroupEntity attrGroup = attrGroupService.getInfoById(attrGroupId);
        return R.ok().put("attrGroup", attrGroup);

    }

    /**
     * 保存新增商品分组属性信息
     * @param attrGroup
     * @return
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);
        return R.ok();
    }

    /**
     * 修改商品分组属性信息
     * @param attrGroup
     * @return
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);
        return R.ok();
    }

    /**
     * 删除或批量删除商品分组信息
     * @param attrGroupIds
     * @return
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeGroupIdsAndRelation(Arrays.asList(attrGroupIds));
        return R.ok();
    }

}
